<?php

class OrderModel extends Model{
	
	protected $trueTableName  = "csl_order";
	protected $trueTableName2 = "csl_user";
	protected $trueTableName3 = "csl_shopping_cart";
	protected $trueTableName4 = "csl_products";
	
	
	function getlist(){
		$order   = $this->select(false);
		$user    = $this->trueTableName2;
		$cart    = $this->trueTableName3;
		$product = $this->trueTableName4;
		
		$result = $this->table("$order a")
					   ->field("a.*,b.username,b.name")
					   ->join("$user b on a.uid = b.uid")
					   ->select();
		
		//购物车查找具体的产品内容
		foreach($result as $k=>$v) {
			$temp = $this->table("$cart a")
						 ->field("a.*,b.pname")
						 ->where("a.order_no = '".$v['order_no']."'")
						 ->join("$product b on a.pid = b.pid")
						 ->select();
			
			$plist = takearr($temp,'pname');
			$result[$k]['cart'] = $plist;
		}
		
		return $result;
	}
	
	//计算共多少盒经典/尊享--------旧版----------待删
	function getcount2() {
		$result = $this->select();
		
		$jingdian = 0;
		$zunxiang = 0;
		if(!empty($result)) {
			foreach($result as $k=>$v) {
				if($v['order_item_price']==200) {
					$jingdian += $v['number'];
				}
				if($v['order_item_price']==280) {
					$zunxiang += $v['number'];
				}
			}
		}
		
		$count = array();
		$count['jingdian'] = $jingdian;
		$count['zunxiang'] = $zunxiang;
		
		return $count;
	}
	
	//计算共多少盒经典/尊享
	function getcount() {
		$temp = $this->select(false);
		
// 		echo $temp;
		
		$result = $this->table("$temp a")->field("a.*,b.pid,b.price,b.quantity")
					   ->join("csl_shopping_cart b on a.order_no = b.order_no")
					   ->select();

// 		dayin($this->getlastsql());
	//	dayin($result);
		
// 		$result = $this->select();
		
		$jingdian = 0;
		$zunxiang = 0;
		$other = 0;
		if(!empty($result)) {
			foreach($result as $k=>$v) {
				if($v['product_outside_type']=='' && $v['product_inside_type']=='') {//有购物车支持的订单，用购物车内容统计
					if($v['pid']==1) {
						$jingdian += $v['quantity'];
					}else if($v['pid']==2) {
						$zunxiang += $v['quantity'];
					}else{
						$other += $v['quantity'];
					}
				} else {//无购物车支持的订单，直接用订单内容统计
					if($v['order_item_price']==200) {
						$jingdian += $v['number'];
					}else if($v['order_item_price']==280) {
						$zunxiang += $v['number'];
					}else{
						$other += $v['number'];
					}
					
				}
				
			}
		}
		
		$count = array();
		$count['jingdian'] = $jingdian;
		$count['zunxiang'] = $zunxiang;
		$count['other'] = $other;
		
// 		print_r($result);
		
		return $count;
	}
	
	
}

?>